﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using System.Net.Sockets;
using System.IO;

namespace OCDStudio.SMSPPPServer
{
    public partial class MainForm : Form
    {
        private bool started = false;
        private SocketServer sock;
        private Thread mainT;

        public MainForm()
        {
            InitializeComponent();
        }

     
        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (!started)
            {
                if (String.IsNullOrEmpty(txtPort.Text))
                {
                    MessageBox.Show("Enter a valid port!", "Error!");
                    return;
                }
                string port = txtPort.Text;
                try
                {
                    int portInt = Int32.Parse(port);
                    if (portInt <= 1024 || portInt >= 65536)
                    {
                        MessageBox.Show("Enter a valid port!", "Error!");
                        return;
                    }
                    else
                    {
                        sock = new SocketServer(portInt);
                        mainT = new Thread(new ThreadStart(sock.StartServer));
                        mainT.Start();
                        started = true;

                        String strHostName = Dns.GetHostName();

                        lstIp.Items.Clear();
                        // Find host by name
                        IPHostEntry iphostentry = Dns.GetHostByName(strHostName);

                        // Enumerate IP addresses
                        
                        foreach (IPAddress ipaddress in iphostentry.AddressList)
                        {
                            lstIp.Items.Add(ipaddress.ToString());
                        }


                        MnuStart.Enabled = false;
                        MnuStop.Enabled = true;
                        lblConnected.Text = "Server Started!";
                        return;
                    }
                }
                catch
                {
                    MessageBox.Show("Enter a valid port!", "Error!");
                    return;
                }

            }





        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            if (started) {
                /*
                TcpClient cl = new TcpClient("localhost", sock.Port);
                StreamWriter wr = new StreamWriter(cl.GetStream());                
                wr.WriteLine("STOP");
                wr.Flush();

                cl.Close();*/
                mainT.Abort();
                sock.StopServer();
                started = false;
                lblConnected.Text = "Server Stopped!";
                MnuStart.Enabled = true;
                MnuStop.Enabled = false;
                lstIp.Items.Clear();
                
                
            }
        }


    }
}